// 下载模板 选择模板使用
// 用过配置文件 获取模板信息 （有哪些模板）
const ora = require('ora');
const inquirer = require('inquirer');
const { repoList, tagList, downloadLocal } = require('./utils/git');

const install = async () => {
  let loading = ora('fetching template ......');
  loading.start();
  let list = await repoList();
  loading.succeed();
  list = list.map(({ name }) => name);
  const answer = await inquirer.prompt([{
    type: 'list',
    name: 'project',
    choices: list,
    questions: 'please choice template',
  }]);
  // 拿到当前项目
  const { project } = answer;
  // 获取当前项目的版本号
  loading = ora('fetch tag ......');
  loading.start();
  list = await tagList(project);
  loading.succeed();
  list = list.map(({ name }) => name);
  const { tag } = answer;
  // 下载文件（先下载到缓存区文件中
  // zf-cli init
  // 下载中 ...
  loading = ora('download project ......');
  loading.start();
  await downloadLocal(project, tag);
  loading.succeed();
};

module.exports = install;
